package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.lei.entity.Book;
import com.lei.entity.LayDate;
import com.lei.service.BookService;
import com.lei.service.impl.BookServiceImpl;
import com.lei.util.BaseDb;
import com.lei.util.PageBean;
import com.lei.util.TransBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/21 18:19
 */
@WebServlet("/bookServlet/*")
public class BookServlet extends BaseServlet {

    private final BookService bookService = new BookServiceImpl();

    /**
     * 功能描述:通过类型Id，搜索该类型的所有书，动态生成书名的下拉框
     *
     * @param request  Req
     * @param response Res
     * @return java.lang.String
     */
    public String getBookIdAndNameByTypeId(HttpServletRequest request, HttpServletResponse response) {
        String typeid = request.getParameter("typeid");
        List<Book> list = bookService.getBookIdAndNameByTypeId(Integer.parseInt(typeid));
        return JSON.toJSONString(list);

    }

    /**
     * 功能描述:根据书的Id获得书的数量和价格，用于出库时验证
     *
     * @param request  Req
     * @param response Res
     * @return java.lang.String
     */
    public String getBookCountAndPriceById(HttpServletRequest request, HttpServletResponse response) {
        String bookid = request.getParameter("bookid");
        Book book = bookService.getBookCountAndPriceById(bookid);
        return JSON.toJSONString(book);
    }

    /**
     * 功能描述:获得所有书
     *
     * @param request  Req
     * @param response Res
     * @return java.lang.String
     */
    public String getAllBook(HttpServletRequest request, HttpServletResponse response) {
        Book book = new Book();
        TransBean.populate(book, request.getParameterMap());
        PageBean<Book> page = bookService.getAllBook(book);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:修改图书的状态，上架---下架
     *
     * @param request  Req
     * @param response Res
     * @return java.lang.String
     */
    public String updateStatus(HttpServletRequest request, HttpServletResponse response) {
        Book book = new Book();
        TransBean.populate(book, request.getParameterMap());
        int result = bookService.updateStatus(book);
        return result + "";
    }

    /**
     * 功能描述:添加和修改图书信息
     *
     * @param request  Req
     * @param response Res
     * @return java.lang.String
     */
    public String addBook(HttpServletRequest request, HttpServletResponse response) {
        Book book = new Book();
        TransBean.populate(book, request.getParameterMap());
        int result = bookService.addBook(book);
        return result + "";
    }

    public String rankingBook(HttpServletRequest request,HttpServletResponse response){
        LayDate layDate = new LayDate();
        TransBean.populate(layDate, request.getParameterMap());
        String sql = "select b.bookname name,sum(cd.count) value from t_ckdetail cd join t_book b on cd.bookid = b.bookid join t_ck c on c.rukuid = cd.rukuid where 1=1 ";
        if (layDate.getYear()!=null){
            sql+=" and year(rdate) = "+layDate.getYear();
        }else{
            sql+=" and year(rdate) = 2020";
        }
        if (layDate.getMonthMin()!=null){
            sql+=" and month(rdate) >="+layDate.getMonthMin();
        }else {
            sql+=" and month(rdate) >= 1";
        }
        if (layDate.getMonthMax()!=null){
            sql+=" and month(rdate) <="+layDate.getMonthMax();
        }else {
            sql+=" and month(rdate) <= 12";
        }
        sql += "  GROUP BY cd.bookid ORDER BY value desc LIMIT 5";
        List<Map> list = BaseDb.query(sql);
        return JSON.toJSONString(list);
    }


    public String rankingPublisher(HttpServletRequest request,HttpServletResponse response){
        LayDate layDate = new LayDate();
        TransBean.populate(layDate,request.getParameterMap());
        // 查找所有的出版社
        String sql2 = "select DISTINCT publisher from t_book ";
        List<Map> query = BaseDb.query(sql2);
        ArrayList<String> publisherList = new ArrayList<>();
        // 将查到的出版社信息封装成一个list
        for (Map map : query) {
            publisherList.add(map.get("publisher")+"");
        }

        // 查询各个出版社的销售信息
        String sql = "SELECT b.publisher,sum( cd.count ) total FROM t_ckdetail cd right JOIN t_book b ON cd.bookid = b.bookid JOIN t_ck c ON c.rukuid = cd.rukuid WHERE 1=1";
        if (layDate.getYear()!=null){
            sql+=" and year(rdate) = "+layDate.getYear();
        }else{
            sql+=" and year(rdate) = 2020";
        }
        if (layDate.getMonthMin()!=null){
            sql+=" and month(rdate) >="+layDate.getMonthMin();
        }else {
            sql+=" and month(rdate) >= 1";
        }
        if (layDate.getMonthMax()!=null){
            sql+=" and month(rdate) <="+layDate.getMonthMax();
        }else {
            sql+=" and month(rdate) <= 12";
        }
        sql += "  GROUP BY b.publisher";
        List<Map> mapList = BaseDb.query(sql);
        // 新建一个list用来装载每个出版社对应的销售额
        ArrayList<Integer> totalList = new ArrayList<>();
        // 循环所有出版社
        for (String s : publisherList) {
            int total = 0;
            for (Map map : mapList) {
                // 如果有销售额的出版社将销售额放到与 出版社对应的list相同的位置 如果该出版社没有销售额，将对应的位置设置为 0
                if (s.equals(map.get("publisher"))){
                    total = Integer.parseInt(map.get("total")+"");
                }
            }
            totalList.add(total);
        }
        Map<String, List> result = new HashMap<>(32);
        result.put("publisher",publisherList);
        result.put("total",totalList);
        return JSON.toJSONString(result);
    }
}
